package net.q1cc.cfs.jamp.event;

/**
 * EventReporter
 * This class is used to add Listeners to. It reports any events
 * like "songEnded" etc, categorizing it into subcategories for performance.
 * 
 * Every class that wants to react to such events needs to implement an JAmpEventListener.
 * below code is copied from javainsel, will update it soon.
 */
import java.util.*; 
import javax.swing.event.EventListenerList; 
 
public class EventReporter
{ 
  /** List of listeners. */ 
  private EnumMap<EventType,EventListenerList> listeners = new EnumMap<EventType,EventListenerList>(EventType.class);
 
 
  /** Radio with frequent ads. */ 
  public EventReporter() 
  { 
    for (EventType e:EventType.values()) {
        listeners.put(e, new EventListenerList());
    }
  } 
 
  /** 
   * Adds an {@code AdListener} to the radio. 
   * @param l the {@code AdListener} to be added 
   */ 
  public void addListener( JAmpEventListener listener,EventType eventType ) 
  { 
    listeners.get(eventType).add(JAmpEventListener.class, listener);
  }
  
  /**
   * Add Listener to all Lists.
   * @param listener 
   */
  public void addListener(JAmpEventListener listener){
      for(EventListenerList l:listeners.values()){
          l.add(JAmpEventListener.class, listener);
      }
  }
 
  /** 
   * Removes an {@code AdListener} from the radio. 
   * @param l the listener to be removed 
   */ 
 
  public void removeListener( JAmpEventListener listener,EventType eventType ) 
  { 
    listeners.get(eventType).remove( JAmpEventListener.class, listener ); 
  } 
 
  /** 
   * Notifies all {@code AdListener}s that have registered interest for 
   * notification on an {@code AdEvent}. 
   * @param event  the {@code AdEvent} object 
   * @see EventListenerList 
   */ 
  public synchronized void throwEvent( JAmpEvent event ) 
  { 
    for ( JAmpEventListener l : listeners.get(event.getType()).getListeners( JAmpEventListener.class ) ) 
      l.handleEvent(event);
  } 
}